package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.SellerDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.SellerEntity;
import io.renren.modules.seller.service.SellerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("sellerService")
public class SellerServiceImpl extends ServiceImpl<SellerDao, SellerEntity> implements SellerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SellerEntity> page = this.page(
                new Query<SellerEntity>().getPage(params),
                new QueryWrapper<SellerEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult findPage(String page, String rows) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);
        IPage<SellerEntity> iPage = this.page(
                new Query<SellerEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public PageResult findPage(SellerEntity seller, String page, String rows) {
        // 分页
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);

        // 条件
        String sellerId = seller.getSellerId();
        String name = seller.getName();
        String nickName = seller.getNickName();
        String status = seller.getStatus();
        String linkmanName = seller.getLinkmanName();
        String linkmanMobile = seller.getLinkmanMobile();

        IPage<SellerEntity> iPage = this.page(
                new Query<SellerEntity>().getPage(params),
                new QueryWrapper<SellerEntity>()
                        .eq(StringUtils.isNotBlank(sellerId), "seller_id", sellerId)
                        .eq(StringUtils.isNotBlank(name), "name", name)
                        .eq(StringUtils.isNotBlank(nickName), "nick_name", nickName)
                        .eq(StringUtils.isNotBlank(status), "status", status)
        );
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public void updateStatus(String sellerId, String status) {
        SellerEntity entity = new SellerEntity();
        entity.setSellerId(sellerId);
        entity.setStatus(status);
        this.updateById(entity);
    }

}
